package example.demo.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class SyncTest {

    static final Object lock = new Object();
    static final CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
    //    static final Semaphore semaphore = new Semaphore(0);
    static int count = 10;
    static final CountDownLatch latch = new CountDownLatch(count);


    public void sync(){
        synchronized (this){

        }
    }
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < count; i++) {
            new Thread(() -> {
                try {
                    log.info(Thread.currentThread().getName() + "----进入赛道，准备出发");
                    cyclicBarrier.await();
                    synchronized (lock) {
                        log.info(Thread.currentThread().getName() + "----执行monitorenter获得了lock实例所对应的monitor所有权");
                        latch.countDown();
                        lock.wait();
                        log.info(Thread.currentThread().getName() + "----notify-gogogo");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, "sync-" + i).start();
        }
        latch.await();
        log.info("10个人进入赛道-----进行下面的活动");
        synchronized (lock) {
            log.info(Thread.currentThread().getName() + "----发放10个许可证");
            lock.notifyAll();
        }
    }
}
